<template>
	<el-dialog :title="!dataForm.id ? '新建物料单位' : '编辑物料单位'" :close-on-click-modal="false" :visible.sync="visible" class="SHZY-dialog SHZY-dialog_center" lock-scroll width="600px">
		<el-row :gutter="15" class="" >
				<el-form ref="elForm" :model="dataForm" size="medium" label-width="100px" label-position="right" :rules="rules">
					<el-col :span="24">
						<el-form-item label="单位名称" prop="fullName">
							<el-input v-model='dataForm.fullName' placeholder='请输入' clearable required :style='{"width":"100%"}' maxlength='50' show-word-limit>
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="单位编号" prop="enCode">
							<el-input v-model='dataForm.enCode' placeholder='请输入' clearable required :style='{"width":"100%"}' maxlength='50' show-word-limit >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="排序" prop="sortCode">
							<el-input-number v-model='dataForm.sortCode' placeholder='数字文本' :step='1' :min="0">
							</el-input-number>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="有效标志" prop="enabledMark">
							<el-switch v-model='dataForm.enabledMark' :active-value='1' :inactive-value='0' >
							</el-switch>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="说明" prop="description">
							<el-input v-model='dataForm.description' placeholder='请输入' show-word-limit :style='{"width":"100%"}' type='textarea' :autosize='{"minRows":4,"maxRows":4}' >
							</el-input>
						</el-form-item>
					</el-col>
				</el-form>
		</el-row>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
	import request from '@/utils/request'
	import { GetInfoFy, CreateMaterialUnit,updateMaterialUnit,} from '@/api/q1/basedata/materialUnit'
	import { getDataInterfaceRes } from '@/api/q1/basedata/materialUnit'
	export default {
		components: {  },
		props: [],
		data() {
			const validatePass = (rule, value, callback) => {
				if (!value || value.trim().length == 0) {
					this.$set(this.dataForm,rule.field,'')
						callback(new Error("不能为空"));
					} else if (value.trim().length > 50) {
						callback(new Error("长度不能超过50字"));
					} else {
						callback();
					}
				};
			return {
				btnLoading:false,
				loading: false,
				visible: false,
				dataForm: {
					id:'',
					parentId:"0",
                    dictionaryTypeId:"459486664230731141",
					fullName:undefined,
					enCode:undefined,
					sortCode:0,
					enabledMark:1,
					description:undefined,
                    
				},
				rules: {
					fullName:[
						{
							required:true,
							message: '请输入单位名称',
							validator:validatePass,
							trigger:'"blur"'
						},
					],
					enCode:[
						{
							required:true,
							message: '请输入单位编号',
							validator:validatePass,
							trigger:'"blur"'
						},
					],
				},
			}
		},
		computed: {},
        watch: {},
        created() {
		},
		mounted() {
        },
		methods: {
			goBack() {
                this.$emit('refresh')
            },
			init(id) {
				this.dataForm.id = id || 0;
                this.visible = true;
				this.$nextTick(() => {
					this.$refs['elForm'].resetFields();
					if (this.dataForm.id) {
							GetInfoFy(this.dataForm.id).then(res => {
						console.log(res.data);
						this.dataForm = res.data;
						if (!this.dataForm.filePath) this.dataForm.filePath = [];
					})
					}
				});
				//this.$store.commit('generator/UPDATE_RELATION_DATA', {})
			},
		dataFormSubmit() {
			this.$refs['elForm'].validate((valid) => {
				if (valid) {
					this.btnLoading = true;
					const formMethod = !this.dataForm.id ? CreateMaterialUnit : updateMaterialUnit;
					formMethod(this.dataForm).then((res) => {
						this.btnLoading = false;
						this.$message({
							message: res.msg,
							type: 'success',
							duration: 1000,
							onClose: () => {
								this.visible = false,
									this.$emit('refresh', true)
							}
						})
					}).catch((res) => {
						this.btnLoading = false;
					})
				}
			})
		},
		}
	}
</script>
